/*
 * $Id$
 * $HeadURL$
 *
 * ==============================================================================
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.jettyrunner.server;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import org.eclipse.jetty.util.log.Log;

/**
 * @author Andrei Solntsev
 * @version $Revision: $ $Date: $ $Author: $
 */
public class Configuration implements Serializable
{
	protected static final String CLASS_VERSION = "$Revision: 1.2 $ $Author: asolntsev $";

	private static final long serialVersionUID = 261299593737798661L;

	private boolean loaded = false;
	private File configFolder;
	private File fJavaProperties;
	private Properties javaProperties;
	private Map<String, Map<String, Properties>> dataSources;

	public Configuration()
	{
		// Default constructor. Call method load() to load properties.
	}

	/*public Configuration(Properties javaProperties, Map<String, Properties> dataSources)
	{
		this.javaProperties = javaProperties;
		this.dataSources = dataSources;
	}*/

	/**
	 * Used for logging/debugging
	 */
	public File getJavaPropertiesFile()
	{
		return fJavaProperties;
	}

	public Properties getJavaProperties()
	{
		return javaProperties;
	}

	public Map<String, Map<String, Properties>> getDataSources()
	{
		return dataSources;
	}

	public Set<String> getInstanceNames()
	{
		return dataSources.keySet();
	}

	public Map<String, Properties> getDataSources(String instanceName)
	{
		return dataSources.get(instanceName);
	}

	public void load() throws IOException
	{
		if (loaded)
			return;

		configFolder = new File(System.getProperty("user.home") + File.separatorChar + "jetty-runner");
		if (!configFolder.exists())
			throw new IllegalStateException("FATAL: Folder " + configFolder + " does not exist");

		loadSystemProperties(configFolder);
		loadDataSources(configFolder);
		loaded = true;
	}

	protected void loadSystemProperties(File configFolder) throws IOException
	{
		fJavaProperties = new File(configFolder, "java.properties");
		if (fJavaProperties.exists())
		{
			this.javaProperties = Utils.loadProperties(fJavaProperties);
		}
	}

	protected void loadDataSources(File configFolder) throws IOException
	{
		dataSources = new HashMap<String, Map<String, Properties>>();

		for (File instance : configFolder.listFiles())
		{
			if (!instance.isDirectory())
				continue;

			final Map<String, Properties> instanceDataSources = new HashMap<String, Properties>();
			dataSources.put(instance.getName(), instanceDataSources);
			for (File ds : instance.listFiles())
			{
				if (!ds.isFile() || !ds.getName().endsWith(".properties"))
					continue;

				String dsName = ds.getName().replaceFirst("\\.properties", "");
				if (!dsName.matches("[\\w\\d\\-]+"))
				{
					Log.warn("Data Source name contains invalid characters: '" + dsName + "'");
				}
				String jdbcJndiName = "java:comp/env/jdbc/"+dsName;

				Properties p = Utils.loadProperties(ds);
				instanceDataSources.put(jdbcJndiName, p);
			}
		}
	}
}
